//
//  RootView.m
//  UI5_UIControll
//
//  Created by TravelRound on 15/11/12.
//  Copyright © 2015年 TravelRound. All rights reserved.
//

#import "RootView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    
    self.backgroundColor = [UIColor lightGrayColor];
    
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 500, 300, 50)];
    
    // 当前值设置(圆扭位置改变)
    //    slider.value = 0.5;
    // 改变最大值
    slider.maximumValue = 2.0;
    // 改变最小值
    //    slider.minimumValue = 2.0;
    
    slider.minimumTrackTintColor = [UIColor blueColor];
    slider.maximumTrackTintColor = [UIColor redColor];
    slider.thumbTintColor = [UIColor greenColor];
    
    // 注意, UIControlEventValueChanged - 改变值时触发
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:slider];
    [slider release];

    
    
    
    NSMutableArray *imageArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 22; i++) {
        
        // 拼接图片的名字
        // 注意!!!仔细查看图片的序号是1还是01/001...
        // 若序号是01的形式, 用%02ld的形式
        NSString *fileName = [NSString stringWithFormat:@"Zombie%ld.tiff", i];
        // 创建UIImage类型对象
        UIImage *image = [UIImage imageNamed:fileName];
        // 将图片添加进数组
        [imageArr addObject:image];
        
    }
    
    
    // UIImageView播放动画
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 200, 500)];
    
    imageV.image = [UIImage imageNamed:@"Zombie0.tiff"];
    imageV.tag = 10001;
    // 要播放的图片数组
    imageV.animationImages = imageArr;
    
    // 播放时间
    imageV.animationDuration = slider.maximumValue - slider.value;
    
    // 重复次数 - 默认是0, 即无限循环
//    imageV.animationRepeatCount = 2;
    
    [self addSubview:imageV];
    
    // 播放动画
    [imageV startAnimating];
    
    // 停止播放动画
//    [imageV stopAnimating];
    
    [imageV release];
    
    UIButton *stopB = [UIButton buttonWithType:UIButtonTypeCustom];
    [stopB setTitle:@"停止" forState:UIControlStateNormal];
    stopB.frame = CGRectMake(50, 50, 50, 50);
    stopB.backgroundColor = [UIColor redColor];
    [stopB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:stopB];
    
    UIButton *startB = [UIButton buttonWithType:UIButtonTypeCustom];
    [startB setTitle:@"开始" forState:UIControlStateNormal];
    startB.frame = CGRectMake(50, 150, 50, 50);
    startB.backgroundColor = [UIColor redColor];
    [startB addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:startB];
    
    
    UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"王宁", @"杨忠祥", @"初旭", nil]];
    segmentC.frame = CGRectMake(100, 400, 200, 30);
    
    // 设置选择状态
    segmentC.selectedSegmentIndex = 1;
    
    // 插入
    [segmentC insertSegmentWithTitle:@"郎学东" atIndex:1 animated:YES];
    
    // 修改
    [segmentC setTitle:@"王宁宁" forSegmentAtIndex:0];
    
    // 插入带图片的segment
    UIImage *image = [UIImage imageNamed:@"image1.png"];
    
    // 图片渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 边框颜色
    segmentC.tintColor = [UIColor redColor];
    // 背景颜色
    segmentC.backgroundColor = [UIColor blackColor];
    
    
    [segmentC insertSegmentWithImage:image atIndex:2 animated:YES];
    
    [segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:segmentC];
    [segmentC release];
    

    
    
}

- (void)segmentAction:(UISegmentedControl *)sender
{

    NSLog(@"当前选择的下标:%ld", sender.selectedSegmentIndex);
    
}

- (void)button1Action:(UIButton *)sender
{
    
    UIImageView *imageV = (UIImageView *)[self viewWithTag:10001];
    [imageV startAnimating];
    
}


- (void)buttonAction:(UIButton *)sender
{
    
    UIImageView *imageV = (UIImageView *)[self viewWithTag:10001];
    [imageV stopAnimating];
    
}

- (void)sliderAction:(UISlider *)sender
{
    
    NSLog(@"当前滑动条的值为:%f", sender.value);
    
    UIImageView *imageV = (UIImageView *)[self viewWithTag:10001];
    imageV.animationDuration = sender.maximumValue - sender.value + 0.01;
    [imageV startAnimating];
    
}

@end
